package com.imedway.ihe.dtz.xds.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.imedway.ihe.dd.vo.FindDocumentsVO;
import com.imedway.ihe.dd.vo.FindFoldersVO;
import com.imedway.ihe.dd.vo.FindSubmissionSetsVO;
import com.imedway.ihe.dd.vo.GetAssociationsVO;
import com.imedway.ihe.dd.vo.GetDocumentsAndAssociationsVO;
import com.imedway.ihe.dd.vo.GetSubmissionSetsVO;
import com.imedway.ihe.dtz.xds.entity.XdsAssociation;
import com.imedway.ihe.dtz.xds.entity.XdsClassification;
import com.imedway.ihe.dtz.xds.entity.XdsDescription;
import com.imedway.ihe.dtz.xds.entity.XdsExternalidentifier;
import com.imedway.ihe.dtz.xds.entity.XdsExtrinsicobject;
import com.imedway.ihe.dtz.xds.entity.XdsFileInfo;
import com.imedway.ihe.dtz.xds.entity.XdsName;
import com.imedway.ihe.dtz.xds.entity.XdsRegistrypackage;
import com.imedway.ihe.dtz.xds.entity.XdsSlot;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz <br/>
 * @date 2024/7/4
 */
@Slf4j
@Repository
public class BaseDao {
	
	@Resource
	private JdbcTemplate jdbcTemplate;
		
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	public List<String> findExtrinsicobject(FindDocumentsVO vo )  {
		
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT distinct parent FROM t_xds_slot where 1=1 ");
		if (StringUtils.hasText(vo.get$XDSDocumentEntryPatientId())) {
			sql.append("  and name_='sourcePatientId' ");
			sql.append("  and value=:value ");
			map.put("value", vo.get$XDSDocumentEntryPatientId());
		}

		log.info(sql.toString());
		List<String> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, String.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<String> FIND_SUBMISSION_SETS(FindSubmissionSetsVO vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT distinct parent FROM t_xds_slot where 1=1 ");
		if (StringUtils.hasText(vo.get$XDSSubmissionSetPatientId())) {
			sql.append("  and name_='sourcePatientId' ");
			sql.append("  and value=:value ");
			map.put("value", vo.get$XDSSubmissionSetPatientId());
		}
		log.info(sql.toString());
		List<String> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, String.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<String> FIND_FOLDERS(FindFoldersVO vo)  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT distinct parent FROM t_xds_slot where 1=1 ");
		if (StringUtils.hasText(vo.get$XDSFolderPatientId())) {
			sql.append("  and name_='sourcePatientId' ");
			sql.append("  and value=:value ");
			map.put("value", vo.get$XDSFolderPatientId());
		}
		log.info(sql.toString());
		List<String> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, String.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsExtrinsicobject> GET_Extrinsicobject(GetDocumentsAndAssociationsVO vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_extrinsicobject where 1=1 ");
		if (StringUtils.hasText(vo.get$homeCommunityId())) {
			sql.append("  and home=:home ");
			map.put("home", vo.get$homeCommunityId());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryUniqueId())) {
			sql.append("  and lid=:lid ");
			map.put("lid", vo.get$XDSDocumentEntryUniqueId());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryEntryUUID())) {
			sql.append("  and id=:id ");
			map.put("id", vo.get$XDSDocumentEntryEntryUUID());
		}
		log.info(sql.toString());
		List<XdsExtrinsicobject> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsExtrinsicobject.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsAssociation> GET_associations(XdsAssociation vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_association where 1=1 ");
		if (StringUtils.hasText(vo.getId())) {
			sql.append("  and id=:id ");
			map.put("id", vo.getId());
		}
		if (StringUtils.hasText(vo.getSourceObject())) {
			sql.append("  and sourceObject=:sourceObject ");
			map.put("sourceObject", vo.getSourceObject());
		}
		if (StringUtils.hasText(vo.getTargetObject())) {
			sql.append("  and targetObject=:targetObject ");
			map.put("targetObject", vo.getTargetObject());
		}
		log.info(sql.toString());
		List<XdsAssociation> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsAssociation.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsRegistrypackage> GET_registrypackage(XdsRegistrypackage vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_registrypackage where 1=1 ");
		if (StringUtils.hasText(vo.getId())) {
			sql.append("  and id=:id ");
			map.put("id", vo.getId());
		}
		if (StringUtils.hasText(vo.getId())) {
			sql.append("  and id=:id ");
			map.put("id", vo.getId());
		}
		if (StringUtils.hasText(vo.getHome())) {
			sql.append("  and home=:home ");
			map.put("home", vo.getHome());
		}
		if (StringUtils.hasText(vo.getStatus())) {
			sql.append("  and status=:status ");
			map.put("status", vo.getStatus());
		}
		log.info(sql.toString());
		List<XdsRegistrypackage> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsRegistrypackage.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsSlot> GET_slot(XdsSlot vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_slot where 1=1 ");
		if (StringUtils.hasText(vo.getName_())) {
			sql.append("  and name_=:name_ ");
			map.put("name_", vo.getName_());
		}
		if (StringUtils.hasText(vo.getParent())) {
			sql.append("  and parent=:parent ");
			map.put("parent", vo.getParent());
		}
		log.info(sql.toString());
		List<XdsSlot> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsSlot.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsDescription> GET_description(XdsDescription vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_description where 1=1 ");
		if (StringUtils.hasText(vo.getParent())) {
			sql.append("  and parent=:parent ");
			map.put("parent", vo.getParent());
		}
		log.info(sql.toString());
		List<XdsDescription> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsDescription.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsName> GET_name(XdsName vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_name where 1=1 ");
		if (StringUtils.hasText(vo.getParent())) {
			sql.append("  and parent=:parent ");
			map.put("parent", vo.getParent());
		}
		log.info(sql.toString());
		List<XdsName> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsName.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	public List<XdsClassification> GET_classification(XdsClassification vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_classification where 1=1 ");
		if (StringUtils.hasText(vo.getClassifiedObject())) {
			sql.append("  and classifiedObject=:classifiedObject ");
			map.put("classifiedObject", vo.getClassifiedObject());
		}
		log.info(sql.toString());
		List<XdsClassification> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsClassification.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsExternalidentifier> GET_externalidentifier(XdsExternalidentifier vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_externalidentifier where 1=1 ");
		if (StringUtils.hasText(vo.getRegistryObject())) {
			sql.append("  and registryObject=:registryObject ");
			map.put("registryObject", vo.getRegistryObject());
		}
		if (StringUtils.hasText(vo.getObjectType())) {
			sql.append("  and objectType=:objectType ");
			map.put("objectType", vo.getObjectType());
		}
		if (StringUtils.hasText(vo.getIdentificationScheme())) {
			sql.append("  and identificationScheme=:identificationScheme ");
			map.put("identificationScheme", vo.getIdentificationScheme());
		}
		log.info(sql.toString());
		List<XdsExternalidentifier> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsExternalidentifier.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsAssociation> GET_ASSOCIATIONS(GetAssociationsVO vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_association where 1=1 ");
		if (StringUtils.hasText(vo.get$uuid())) {
			sql.append("  and id=:id ");
			map.put("id", vo.get$uuid());
		}
		if (StringUtils.hasText(vo.get$homeCommunityId())) {
			sql.append("  and lid=:lid ");
			map.put("lid", vo.get$homeCommunityId());
		}
		log.info(sql.toString());
		List<XdsAssociation> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsAssociation.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsRegistrypackage> GET_SUBMISSION_SETS(GetSubmissionSetsVO vo )  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM t_xds_registrypackage where 1=1 ");
		if (StringUtils.hasText(vo.get$uuid())) {
			sql.append("  and id=:id ");
			map.put("id", vo.get$uuid());
		}
		if (StringUtils.hasText(vo.get$homeCommunityId())) {
			sql.append("  and lid=:lid ");
			map.put("lid", vo.get$homeCommunityId());
		}
		log.info(sql.toString());
		List<XdsRegistrypackage> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsRegistrypackage.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public void saveExtrinsicobject(XdsExtrinsicobject xdsObject)  {		
		
		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_extrinsicobject (");
		sql.append("id,home,lid,objectType,status,versionName,comment_,isOpaque,mimeType,contentVersionName,contentVersionComment");
		sql.append(") VALUES (");
		sql.append(":id,:home,:lid,:objectType,:status,:versionName,:comment_,:isOpaque,:mimeType,:contentVersionName,:contentVersionComment");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("id", xdsObject.getId());
	    parameters.put("home", xdsObject.getHome());
	    parameters.put("lid", xdsObject.getLid());
	    parameters.put("objectType", xdsObject.getObjectType());
	    parameters.put("status", xdsObject.getStatus());
	    parameters.put("versionName", xdsObject.getVersionName());
	    parameters.put("comment_", xdsObject.getComment_());
	    parameters.put("isOpaque", xdsObject.getIsOpaque());
	    parameters.put("mimeType", xdsObject.getMimeType());
	    parameters.put("contentVersionName", xdsObject.getContentVersionName());
	    parameters.put("contentVersionComment", xdsObject.getContentVersionComment());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);

	}
	
	public void saveRegistrypackage(XdsRegistrypackage xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_registrypackage (");
		sql.append("id,home,lid,objectType,status,versionName,comment_");
		sql.append(") VALUES (");
		sql.append(":id,:home,:lid,:objectType,:status,:versionName,:comment_");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("id", xdsObject.getId());
	    parameters.put("home", xdsObject.getHome());
	    parameters.put("lid", xdsObject.getLid());
	    parameters.put("objectType", xdsObject.getObjectType());
	    parameters.put("status", xdsObject.getStatus());
	    parameters.put("versionName", xdsObject.getVersionName());
	    parameters.put("comment_", xdsObject.getComment_());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveAssociation(XdsAssociation xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_association (");
		sql.append("id,home,lid,objectType,status,versionName,comment_,associationType,sourceObject,targetObject");
		sql.append(") VALUES (");
		sql.append(":id,:home,:lid,:objectType,:status,:versionName,:comment_,:associationType,:sourceObject,:targetObject");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("id", xdsObject.getId());
	    parameters.put("home", xdsObject.getHome());
	    parameters.put("lid", xdsObject.getLid());
	    parameters.put("objectType", xdsObject.getObjectType());
	    parameters.put("status", xdsObject.getStatus());
	    parameters.put("versionName", xdsObject.getVersionName());
	    parameters.put("comment_", xdsObject.getComment_());
	    parameters.put("associationType", xdsObject.getAssociationType());
	    parameters.put("sourceObject", xdsObject.getSourceObject());
	    parameters.put("targetObject", xdsObject.getTargetObject());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsSlot(XdsSlot xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_slot (");
		sql.append("sequenceId,name_,slotType,value,parent");
		sql.append(") VALUES (");
		sql.append(":sequenceId,:name_,:slotType,:value,:parent");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("sequenceId", xdsObject.getSequenceId());
	    parameters.put("name_", xdsObject.getName_());
	    parameters.put("slotType", xdsObject.getSlotType());
	    parameters.put("value", xdsObject.getValue());
	    parameters.put("parent", xdsObject.getParent());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsClassification(XdsClassification xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_classification (");
		sql.append("id,home,lid,objectType,status,versionName,comment_,classificationNode,classificationScheme,classifiedObject,nodeRepresentation");
		sql.append(") VALUES (");
		sql.append(":id,:home,:lid,:objectType,:status,:versionName,:comment_,:classificationNode,:classificationScheme,:classifiedObject,:nodeRepresentation");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("id", xdsObject.getId());
	    parameters.put("home", xdsObject.getHome());
	    parameters.put("lid", xdsObject.getLid());
	    parameters.put("objectType", xdsObject.getObjectType());
	    parameters.put("status", xdsObject.getStatus());
	    parameters.put("versionName", xdsObject.getVersionName());
	    parameters.put("comment_", xdsObject.getComment_());
	    parameters.put("classificationNode", xdsObject.getClassificationNode());
	    parameters.put("classificationScheme", xdsObject.getClassificationScheme());
	    parameters.put("classifiedObject", xdsObject.getClassifiedObject());
	    parameters.put("nodeRepresentation", xdsObject.getNodeRepresentation());
		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsExternalidentifier(XdsExternalidentifier xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_externalidentifier (");
		sql.append("id,home,lid,objectType,status,versionName,comment_,registryObject,identificationScheme,value");
		sql.append(") VALUES (");
		sql.append(":id,:home,:lid,:objectType,:status,:versionName,:comment_,:registryObject,:identificationScheme,:value");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("id", xdsObject.getId());
	    parameters.put("home", xdsObject.getHome());
	    parameters.put("lid", xdsObject.getLid());
	    parameters.put("objectType", xdsObject.getObjectType());
	    parameters.put("status", xdsObject.getStatus());
	    parameters.put("versionName", xdsObject.getVersionName());
	    parameters.put("comment_", xdsObject.getComment_());
	    parameters.put("registryObject", xdsObject.getRegistryObject());
	    parameters.put("identificationScheme", xdsObject.getIdentificationScheme());
	    parameters.put("value", xdsObject.getValue());
		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsDescription(XdsDescription xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_description (");
		sql.append("charset,lang,value,parent");
		sql.append(") VALUES (");
		sql.append(":charset,:lang,:value,:parent");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("charset", xdsObject.getCharset());
	    parameters.put("lang", xdsObject.getLang());
	    parameters.put("value", xdsObject.getValue());
	    parameters.put("parent", xdsObject.getParent());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsName(XdsName xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_name (");
		sql.append("charset,lang,value,parent");
		sql.append(") VALUES (");
		sql.append(":charset,:lang,:value,:parent");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("charset", xdsObject.getCharset());
	    parameters.put("lang", xdsObject.getLang());
	    parameters.put("value", xdsObject.getValue());
	    parameters.put("parent", xdsObject.getParent());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsFileInfo(XdsFileInfo xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO t_xds_file_info (");
		sql.append("dataId,createTime,updateTime,patientId,mimeType,documentUniqueId,documentRepositoryId,homeCommunityId,oldDocumentUniqueId,"
				+ "oldDocumentRepositoryId,fileName,filePath,fileContent,lid,documentSize,hash");
		sql.append(") VALUES (");
		sql.append(":dataId,:createTime,:updateTime,:patientId,:mimeType,:documentUniqueId,:documentRepositoryId,:homeCommunityId,:oldDocumentUniqueId,"
				+ ":oldDocumentRepositoryId,:fileName,:filePath,:fileContent,:lid,:documentSize,:hash");
		sql.append(")");
		
	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("dataId", xdsObject.getDataId());
	    parameters.put("createTime", xdsObject.getCreateTime());
	    parameters.put("updateTime", xdsObject.getUpdateTime());
	    parameters.put("patientId", xdsObject.getPatientId());
	    parameters.put("mimeType", xdsObject.getMimeType());
	    parameters.put("documentUniqueId", xdsObject.getDocumentUniqueId());
	    parameters.put("documentRepositoryId", xdsObject.getDocumentRepositoryId());
	    parameters.put("homeCommunityId", xdsObject.getHomeCommunityId());
	    parameters.put("oldDocumentUniqueId", xdsObject.getOldDocumentUniqueId());
	    parameters.put("oldDocumentRepositoryId", xdsObject.getOldDocumentRepositoryId());
	    parameters.put("fileName", xdsObject.getFileName());
	    parameters.put("filePath", xdsObject.getFilePath());
	    parameters.put("fileContent", xdsObject.getFileContent());
	    parameters.put("lid", xdsObject.getLid());
	    parameters.put("documentSize", xdsObject.getDocumentSize());
	    parameters.put("hash", xdsObject.getHash());
		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
}
